package com.zczc.tagmanagement.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zczc.tagmanagement.dao.TagDao;
import com.zczc.tagmanagement.dto.TagSortsDTO;
import com.zczc.tagmanagement.entity.Tag;
import com.zczc.tagmanagement.mapper.TagMapper;
import com.zczc.tagmanagement.vo.TagVO2;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 9:46 2018-12-10
 * @Modify by:
 */
@Repository
public class TagDaoImpl implements TagDao {

  @Autowired
  private TagMapper tagMapper;

  @Override
  public Integer addTag(Tag tag) {
    return tagMapper.insertSelective(tag);
  }

  @Override
  public Integer deletedTagLogic(List<String> ids) {
    Example example=new Example(Tag.class);
    Criteria criteria = example.createCriteria();
    criteria.andIn("id",ids);
    Tag tag=new Tag();
    tag.setDeleted(true);
    return tagMapper.updateByExampleSelective(tag,example);
  }

  @Override
  public PageInfo<TagVO2>  selectTagsBySrots(TagSortsDTO sortsDTO,Integer startPage,Integer pageSize) {
    PageHelper.startPage(startPage,pageSize);
    return new PageInfo<>(tagMapper.selectTagsBySrotsq(sortsDTO));
  }

  @Override
  public Integer updateTagsSelective(Tag tag) {
    return tagMapper.updateByPrimaryKeySelective(tag);
  }

  @Override
  public PageInfo<Tag> selectAllTagsByPage(Tag tag,Integer pageSize, Integer startPage) {
    PageHelper.startPage(startPage,pageSize);
    Example example=new Example(Tag.class);
    Criteria criteria = example.createCriteria();
    criteria.andEqualTo("deleted",false);
    if(tag!=null){
      if(tag.getTagName()!=null){
        criteria.andLike("tagName","%"+tag.getTagName()+"%");
      }
    }
    return PageInfo.of(tagMapper.selectByExample(example));
  }
}
